<template>
	<view class="content">
		<view class="add-box">
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="150" label="作业点">
					<u-input v-model="operationId" @click="selectZShow = true" placeholder="请输选择作业点" type="select" />
				</u-form-item>
				<u-form-item label-width="150" label="卸土点">
					<u-input v-model="unloadId" @click="selectXShow = true" placeholder="请输选择卸土点" type="select" />
				</u-form-item>
				<u-form-item label-width="150" label="单次费用">
					<u-input v-model="form.cost" placeholder="请输入单次运输费用" />
				</u-form-item>
<!-- 				<u-form-item label-width="150" label="运输范围">
					<u-input v-model="form.range" placeholder="请输入运输范围" />
				</u-form-item> -->
				<u-form-item :border-bottom="false" label="是否启用" prop="status" label-width="150">
					<u-switch v-model="status" slot="right"></u-switch>
				</u-form-item>
			</u-form>
		</view>
		<view class="button-box">
			<u-button type="primary" @tap="addPath" :disabled="isclick">添加</u-button>
		</view>
		<view class="nav" v-show="isclick">
			<navigator url="/pages/project/pointMangement/addSite/index" hover-class="navigator-hover">
				<view>没有作业点？前往添加 &nbsp; <u-icon name="arrow-right"></u-icon></view>
			</navigator>
		</view>
		<u-select mode="single-column" :list="selectZList" v-model="selectZShow" @confirm="selectZConfirm"></u-select>
		<u-select mode="single-column" :list="selectXList" v-model="selectXShow" @confirm="selectXConfirm"></u-select>
	</view>
</template>

<script>
	import {
		getSite
	} from "@/api/site.js"
	import {
		createPath
	} from "@/api/path.js"
	export default {
		data() {
			return {
				form: {
					operationId: '',
					unloadId: '',
					status: null,
					// range: null,
					projectId: null,
					cost: null
				},
				isclick: false,
				// 显示选择器
				operationId: '',
				unloadId: '',
				// 显示开关
				status: true,
				// 两个选择器的数据
				selectZList: [],
				selectXList: [],
				// 控制选择器弹出
				selectZShow: false,
				selectXShow: false,
				selectList: [{
						value: '郑飞',
						label: '郑飞'
					},
					{
						value: '郑飞',
						label: '郑飞'
					},
					{
						value: '郑飞',
						label: '郑飞'
					}
				]
			};
		},
		created() {
			this.getSiteData()
		},
		onShow() {
			var pages = getCurrentPages();
			this.prevPage = pages[pages.length - 1];
			if (this.prevPage.brand === true) {
				this.getSiteData()
			}
		},
		watch: {
			status: {
				handler(val) {
					if (val) {
						this.form.status = 1
					} else {
						this.form.status = 0
					}
				},
				immediate: true
			}
		},
		methods: {
			// 选择商品类型回调
			selectZConfirm(e) {
				this.form.operationId = '';
				e.map((val, index) => {
					this.form.operationId = val.value
					this.operationId = val.label
				});
			},
			selectXConfirm(e) {
				this.form.unloadId = '';
				e.map((val, index) => {
					this.form.unloadId = val.value
					this.unloadId = val.label
				});
			},
			getSiteData() {
				this.selectXList = []
				this.selectZList = []
				getSite({
					projectId: uni.getStorageSync('id')
				}).then(res => {
					if (res.code !== 1) {
						uni.showToast({
							title: '数据获取失败',
							icon: "none"
						})
						return
					}
					if(res.result.length === 0){
						this.isclick = true
						return
					}
					var zuo = 0;
					var xie = 0
					res.result.forEach((item, index) => {
						let lemp = {}
						lemp.label = item.name
						lemp.value = item.id
						if (item.type === '1') {
							this.selectZList.push(lemp)
							zuo = 1
						} else if (item.type === '2') {
							this.selectXList.push(lemp)
							xie = 1
						}
					})
					if (res.result.length === 0) {
						// console.log("没有东西")
						// this.isclick = true
						// 	uni.showModal({
						// 		title: "提示",
						// 		content: "未检测到工程点位，请添加",
						// 		showCancel: false,
						// 		success: res =>{
						// 			if(res.confirm){
						// 				uni.navigateTo({
						// 					url:'/pages/project/pointMangement/addSite/index'
						// 				})
						// 			}
						// 		}
						// 	})
						// 	return
						// } else if(zuo === 0){
						// 	uni.showModal({
						// 		title: "提示",
						// 		content: "缺少作业点，请添加",
						// 		showCancel: false,
						// 		success: res =>{
						// 			if(res.confirm){
						// 				uni.navigateTo({
						// 					url:'/pages/project/pointMangement/addSite/index'
						// 				})
						// 			}
						// 		}
						// 	})
						// 	return
						// } else if(xie === 0){
						// 	uni.showModal({
						// 		title: "提示",
						// 		content: "缺少卸土点，请添加",
						// 		showCancel: false,
						// 		success: res =>{
						// 			if(res.confirm){
						// 				uni.navigateTo({
						// 					url:'/pages/project/pointMangement/addSite/index'
						// 				})
						// 			}
						// 		}
						// 	})
						// 	return
						// }
					}
				})
				console.log(this.selectXList)

			},
			addPath() {
				if (this.form.operationId === '' || this.form.unloadId === '' || this.form.cost === null ) {
					uni.showToast({
						title: "值不能为空",
						icon: "none",
						duration: 2000
					})
					return
				}
				this.form.projectId = uni.getStorageSync('id')
				createPath(this.form).then(res => {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					// console.log(res)
					if (res.code !== 1) {
						return
					}
					setTimeout(() => {
						var item = true; //item为要传的值
						var pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						prevPage.brand = item;
						uni.navigateBack()
					}, 500)

				})
				console.log(this.form)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx;

		.add-box {
			background-color: #ffffff;
			border-radius: 10rpx;
			padding: 20rpx;
		}

		.button-box {
			margin-top: 80rpx;
		}
	}
	.nav{
		text-align: center;
		margin-top: 100rpx;
		color: blue;
	}
</style>
